<?php
namespace app\daifast\controller;
use \think\Db;
use think\Session;


class Goods extends \think\Controller
{ 
    /**
     * 商品列表
     */
    public function index(){
         
        $title = addslashes(input('title')); 
        $category_id = intval(input('category_id'));
        $status = intval(input('status'));
        //获取商品分类
        $cArr = Db('baode_goods_category')->select();
        $catagoryArr = array_column($cArr, "names","id");
       
        //查询条件
        $where = " 1=1 ";
        $title and $where .=" and title like  '%".$title."%'"; 
        $category_id and $where .=" and category_id = ".$category_id; 
        $status and $where .=" and status = ".$status;
        
        $pageQueryArr =["query"=>["title"=>$title,"category_id"=>$category_id,"status"=>$status]];
        $data = Db('baode_goods')->where($where)->order("id desc")->paginate(10,$pageQueryArr); 
        $listArr = $data->items();
        $pages = $data->render(); 
        
        return $this->allFetch(get_defined_vars());
    }
     
    /**
     * 删除
     */
    public function delete(){
        $gid = intval(input('gid'));       
        Db('baode_goods')->where(['id'=>$gid])->delete();
        $this->showMsg('删除成功', url('Goods/index'));
    }
    
    
    /**
     * 添加商品
     * @return mixed
     */
    public function add(){
      //获取商品信息
      $gid = intval(input('gid'));
      $gid and $goodsInfo = Db('baode_goods')->where(['id'=>$gid])->find();
      //获取商品分类
      $cArr = Db('baode_goods_category')->select();
      $catagoryArr = array_column($cArr, "names","id");
      //编辑或添加
      if(input('sbt')){ 
            $data['title'] = input('title');
            $data['pic'] = input('pic');
            $data['category_id'] = intval(input('category_id'));
            $data['presale_price'] = input('presale_price');
            $data['actual_price'] = input('actual_price');
            $data['standard'] = input('standard');
            $data['code'] = input('code');
            $data['is_hot'] = intval(input('is_hot')); 
            $data['status'] = intval(input('status'));
            $data['click_num'] = intval(input('click_num'));
            $gid or $data['on_time'] = time();
            $data['about_content'] = htmlspecialchars(input('about_content'));
            //鞋库
            $gid and Db('baode_goods')->where(['id'=>$gid])->update($data); 
            $gid or Db('baode_goods')->insert($data); 
            $this->showMsg('操作成功', url('Goods/index'));
        }
        
        return $this->allFetch(get_defined_vars());
    }
    
    
    /**
     * 商品分类管理
     * @return mixed
     */
    public function category(){
        //获取商品分类
        $listArr = Db('baode_goods_category')->select(); 
        return $this->allFetch(get_defined_vars());
    }
    
    /**
     * 商品分类添加
     * @return mixed
     */
    public function categoryAdd(){
        $data['names'] = input('names');
        $data['on_time'] = time();
        $lastId = Db('baode_goods_category')->insertGetId($data);
        $html = '
                <tr id="tr_'.$lastId.'">
                <td>'.$data['names'].'</td>
                <td>
                <a href="javascript:void(0);" data="'.url('Goods/categoryEdit',['cid'=>$lastId]).'" class="tablelink" onclick="editCategory(this)">编辑</a> 
                <a href="'.url('Goods/categoryDelete',['cid'=>$lastId]).'" class="tablelink" >删除</a></td>
                </tr>'
        ; 
        echojson('ok', 1,$html);
    }
    
    
    /**
     * 商品分类删除
     * @return mixed
     */
    public function categoryDelete(){
        
        $cid = intval(input('cid'));
        
        Db('baode_goods_category')->where(['id'=>$cid])->delete();
    
        $this->showMsg('操作成功', url('Goods/category'));
    }
    
    
    
    /**
     * 商品分类添加
     * @return mixed
     */
    
    public function categoryEdit(){
        
        
        $cid = intval(input('cid'));
        
        if(input('ajax')){
            $category = input('category');
            Db('baode_goods_category')->where(['id'=>$cid])->update(['names'=>$category]);
            echojson('ok', 1);
        }
        
        $categoryInfo = Db('baode_goods_category')->where(['id'=>$cid])->find();
        
        return $this->allFetch(get_defined_vars());
    }
    
    
    
    
}
